package maple.core.contextproviders;

import java.util.Vector;

import maple.core.ContextProvider;
import maple.util.Context;
import maple.util.Sensor;

public class SimpleMonitorState extends ContextProvider
{
	private Vector< Double > _thresholds;

	public SimpleMonitorState()
	{
		super( 2 );

		// from Consumer
		_preconditions.add( Sensor.Type.III_ACTIVEPOWER.toString() ); 
		_preconditions.add( Sensor.Type.III_POWERFACTOR.toString() ); 
		_postcondition = Context.Type.MONITORSTATE.toString();

		_thresholds = new Vector< Double >();
	}

	protected void setup()
	{
		super.setup();

		for( int i = 1; i < _arguments.length; ++i )
		{
			_thresholds.add( Double.parseDouble( _arguments[ i ] ) );
		}
	}

	protected String interpret()
	{
		String ap = _preconditions.get( 0 );
		String pf = _preconditions.get( 1 );
		double active_power = Double.parseDouble( Sensor.toString( ap, _perceptions.get( ap ) ) );
		double power_factor = Double.parseDouble( Sensor.toString( pf, _perceptions.get( pf ) ) );
	
		if( active_power < 0 || power_factor < 0 )
		{
			return ContextProvider.State.UNKNOWN.toString();
		}
		else if( power_factor < _thresholds.get( 1 ) )
		{
			return Context.MonitorState.IDLE.toString();
		}
		else if( active_power < _thresholds.get( 0 ) )
		{
			return Context.MonitorState.OFF.toString();
		}
		return Context.MonitorState.ON.toString();
	}
}
